var $ = require("../../utils/Ajax.js");
Page({
  data: {
    Id: null,
    UserInfor: null,
    GratuityMoney: [2,6,8,66,88],
    GratuityUser: []
  },

  onLoad: function (options) {
    var that = this;
    that.setData({
      Id: options.Id
    })
    wx.setNavigationBarTitle({
      title: "打赏"
    }) 
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ff2f3f'
    });

    $.Ajax("api/userRewardList","GET",{
      id: Number(that.data.Id)
    }, function (res) {
      console.log(res)
      if(res.s){
        that.setData({
          GratuityUser: res.d.avatarUrls
        })
        console.log(res)
      }else{
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 2000
        })
      }
      if (res.m == "余额不足") {
        setTimeout(function () {
          wx.navigateTo({
            url: '../TopUpBeans/TopUpBeans',
          })
        }, 2000)
      }
    })
  },

  onShow: function () {
    var Token = wx.getStorageSync('Token');
    var UserInfor = wx.getStorageSync('UserInfor');
    if (Token == '' || Token == null) {
      wx.removeStorageSync('Token');
      wx.removeStorageSync('UserInfor');
      wx.showToast({
        title: "请重新登录",
        icon: 'none',
        duration: 2000
      })
    } else {
      this.setData({
        UserInfor: UserInfor
      })
    }
  },

  // 点击打赏金额
  ClickGratuity: function(e){
    var that = this;
    var Money = e.currentTarget.dataset.money;
    $.Ajax("api/userReward","POST",{
      reward: Money,
      informationId: Number(that.data.Id)
    },function(res){
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 2000
      })
      if (res.m == "余额不足") {
        setTimeout(function () {
          wx.navigateTo({
            url: '../TopUpBeans/TopUpBeans',
          })
        }, 2000)
      }
    })
  },

  // 输入打赏金钱
  GratuityMoney: function(e){
    var that = this;
    var Money = e.detail.value;
    var IsNumber = /^[1-9]+[0-9]*]*$/;
    if (IsNumber.test(Money)){
      $.Ajax("api/userReward", "POST", {
        reward: Money,
        informationId: Number(that.data.Id)
      }, function (res) {
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 2000
        })
        if (res.m == "余额不足") {
          setTimeout(function () {
            wx.navigateTo({
              url: '../TopUpBeans/TopUpBeans',
            })
          }, 2000)
        }
      })
    }else{
      wx.showToast({
        title: '打赏金额只能为1元的整数倍',
        icon: 'none',
        duration: 2000
      })
    }
  }
})